package com.qf.service.impl;

import com.qf.dao.TbPermissionRepository;
import com.qf.pojo.entity.TbPermission;
import com.qf.pojo.request.TbPermissionReq;
import com.qf.pojo.response.BaseResponse;
import com.qf.service.TbPermissionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class TbPermissionServiceImpl implements TbPermissionService {

    @Autowired
    TbPermissionRepository tbPermissionRepository;
    /**
     * 查询所有管理员权限
     *
     * @param page 当前页
     * @param size 当前页显示行数
     * @return 响应对象
     */

    @Override
    public BaseResponse findAll(Integer page, Integer size) {
        PageRequest of = PageRequest.of(page - 1, size);
        Page<TbPermission> all = tbPermissionRepository.findAll(of);
        List<TbPermission> list = all.getContent();
        long total = all.getTotalElements();
        return new BaseResponse(1,"success",list,total);
    }

    /**
     * 修改或新增管理员权限
     *
     * @param tbPermissionReq 权限请求对象
     * @return 响应对象
     */

    @Override
    public BaseResponse updateOrSave(TbPermissionReq tbPermissionReq) {
        TbPermission tbPermission = new TbPermission();
        BeanUtils.copyProperties(tbPermissionReq,tbPermission);
        tbPermissionRepository.saveAndFlush(tbPermission);
        return new BaseResponse().SUCCESS();
    }

    /**
     * 删除某个权限
     * @param prId 权限id
     * @return 响应对象
     */

    @Override
    public BaseResponse deleteById(Integer prId) {
        tbPermissionRepository.deleteById(prId);
        return new BaseResponse().SUCCESS();
    }




}
